#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include"stack_element.h"
#include"stack.h"
struct stack *creat_stack(struct stack_element * elem){
    if(elem==NULL) return NULL;
    else if(elem->data==NULL) return NULL;
    struct stack * stack=(struct stack *)malloc(sizeof(struct stack));
    stack->head=elem;
    stack->tail=elem;
    stack->tail->next=NULL;
    stack->head=stack->tail;
    stack->mount=1;
    return stack;
};

int push_stack(struct stack * stack,struct stack_element *data){
    if(stack==NULL&&data==NULL) return 0;
    struct stack_element * elem=stack_element_creat(data);
    if(stack->head==NULL){
    stack->head=elem;
    stack->tail=elem;
    stack->tail->next=NULL;
    stack->head=stack->tail;
    stack->mount++; 
    return 0; 
    }else{
        elem->next=stack->head;
        stack->head=elem;
        stack->mount++;
        return 0;
    }
}
char ** traverse(struct stack * stack){
    if(stack==NULL) return NULL;
    if(stack->head==NULL) return NULL;
    char ** datas=(char **)malloc(sizeof(char**));
    struct  stack_element * current_node=stack->head;
    int i=0;
    do{
          *(datas+i)=current_node->data;
          i++;
          current_node=current_node->next;

    }while(current_node==NULL);
    *(datas+1)==NULL;
        return datas;
    
}
void traverse_disp(struct stack * stack){
    printf("traverse_disp\n");
    char **datas=traverse(stack);
    if(datas==NULL) return;
    int i=0;
    while(*(datas+i)!=NULL){
        printf("%s\n",*(datas+i));
        i++;
    }
}

void pop_stack_single(struct stack * stack){
    if(stack->mount<1)return ;
    struct stack_element * current_node=stack->head;
    stack->head=stack->head->next;
    if (!stack_element_delete(current_node)){
        stack->mount--;
        if(stack->mount==0) stack->tail=stack->head;
    }
}
void pop_stack(struct stack * stack,int N){
    if(N>stack->mount)return ;
    if(stack->mount<1)return ;
    while(N>0){
        pop_stack_single(stack);
        N--;
    }
}
void delete_stack(struct stack * stack){
    if(stack==NULL || stack->head==NULL) return ;
    pop_stack(stack,stack->mount);
}